package com.zhiyou100.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zhiyou100.mapper.VideoMapper;
import com.zhiyou100.model.Course;
import com.zhiyou100.model.Speaker;
import com.zhiyou100.model.Vi;
import com.zhiyou100.model.Video;
import com.zhiyou100.service.VideoService;
import com.zhiyou100.util.DateUtil;
@Service
public class VideoServiceImpl implements VideoService{
	@Autowired
	private VideoMapper vm;
	@Override
	public Video selectVideo(Integer id) {
		Video video = vm.selectVideo(id);
		Speaker speaker = video.getSpeaker();
		Course course = video.getCourse();
		return video;
	}
	

	@Override
	public List<Video> selectVideoAll() {
		List<Video> videos = vm.selectVideoAll();
		for (Video video : videos) {
			Speaker speaker = video.getSpeaker();
		}
		return videos;
	}
	
	@Override
	public PageInfo<Vi> selectVideoAllAdmin(Integer pageNum,Integer pageSize) {
		if(pageNum==null) {
			pageNum=1;
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Video> videos = vm.selectVideoAll();
		List<Vi> vis = new ArrayList<>();
		
		for (Video video : videos) {
			Vi vi = new Vi();
			
			vi.setTime(DateUtil.getTime(video.getVideo_length()));
			vi.setId(video.getId());
			vi.setVideo_title(video.getVideo_title());
			vi.setSpeaker(video.getSpeaker());
			vi.setCourse(video.getCourse());
			vi.setVideo_image_url(video.getVideo_image_url());
			vi.setVideo_url(video.getVideo_url());
			vi.setVideo_descr(video.getVideo_descr());
			vi.setInsert_time(video.getInsert_time());
			vi.setUpdate_time(video.getUpdate_time());
			vi.setVideo_play_times(video.getVideo_play_times());
			vis.add(vi);
			
		}
		PageInfo<Vi> pageInfo = new PageInfo<>(vis);
		return pageInfo;
	}
	
	@Override
	public PageInfo<Video> selectVideoAllAdmin1(Integer pageNum,Integer pageSize) {
		
		PageHelper.startPage(pageNum, pageSize);
		List<Video> videos = vm.selectVideoAll();
		PageInfo<Video> pageInfo = new PageInfo<>(videos);
		
		return pageInfo;
	}

	@Override
	public PageInfo<Vi> selectByContent(Integer pageNum, Integer pageSize, Video vid) {
		if(pageNum==null) {
			pageNum=1;
		}
		PageHelper.startPage(pageNum, pageSize);
		List<Video> videos = vm.selectByContent(vid);
		List<Vi> vis = new ArrayList<>();
		
		for (Video video : videos) {
			Vi vi = new Vi();
			vi.setTime(DateUtil.getTime(video.getVideo_length()));
			vi.setId(video.getId());
			vi.setVideo_title(video.getVideo_title());
			vi.setSpeaker(video.getSpeaker());
			vi.setCourse(video.getCourse());
			vi.setVideo_image_url(video.getVideo_image_url());
			vi.setVideo_url(video.getVideo_url());
			vi.setVideo_descr(video.getVideo_descr());
			vi.setInsert_time(video.getInsert_time());
			vi.setUpdate_time(video.getUpdate_time());
			vi.setVideo_play_times(video.getVideo_play_times());
			vis.add(vi);
			
		}
		PageInfo<Vi> pageInfo = new PageInfo<>(vis);
		return pageInfo;
	}

	@Override
	public PageInfo<Video> selectByContent1(Integer pageNum, Integer pageSize,Video video) {
		PageHelper.startPage(pageNum, pageSize);
		List<Video> videos = vm.selectByContent(video);
		PageInfo<Video> pageInfo = new PageInfo<>(videos);
		
		return pageInfo;
	}


	@Override
	public int update(Video video) {
		return vm.update(video);
		
	}


	@Override
	public int addVideo(Video video) {
		// TODO Auto-generated method stub
		return vm.addVideo(video);
	}


	@Override
	public int deleteVideo(Integer id) {
		// TODO Auto-generated method stub
		return vm.deleteVideo(id);
	}


	@Override
	public Video selectVideoOnly(Integer id) {
		// TODO Auto-generated method stub
		return vm.selectVideo(id);
	}


	@Override
	public int updateVideo_play_times(Video video) {
		// TODO Auto-generated method stub
		return vm.updateVideo_play_times(video);
	}
	
	
}
